using System.Collections.Generic;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class TargetSelect_RandomUnit : TargetSelect
    {
        public override ETargetSelectType selectType => ETargetSelectType.RandomUnit;

        public override bool selectByUnit => false;

        public int num;
        public bool repeat;

        protected override void OnInitialize(TargetSelectConfig rConfig)
        {
            base.OnInitialize(rConfig);
            if (rConfig.customConfig is TargetSelect_RandomUnit_ConfigData rData)
            {
                this.num = rData.num;
                this.repeat = rData.repeat;
            }
        }

        public override void TrySelect(SelectParam rParam, LVector2 rSelfPos, ref List<Unit> rTargets)
        {
            List<Unit> rAllUnits = ListPool<Unit>.Get();
            TargetSelectUtil.GetAllTargetsByRelation(this.self, this.targetRelation, ref rAllUnits);

            if (rAllUnits != null && rAllUnits.Count > 0)
            {
                for (int i = 0; i < this.num; i++)
                {
                    var nRandom = this.self.battle.random.Range(0, rAllUnits.Count);
                    rTargets.Add(rAllUnits[nRandom]);
                    if (!this.repeat)
                    {
                        rAllUnits.RemoveAt(nRandom);
                    }
                }
            }

            ListPool<Unit>.Put(rAllUnits);
        }

        public override void Free()
        {
            ClassPool.Put(this);
        }
    }
}